﻿package {
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.net.URLRequest;
	import flash.display.Loader;
	
	public class clockskin extends Sprite {
		private var W:uint = 200;
		private var H:uint = 200;
		private var radius_H:uint;
		private var radius_W:uint;
		private var centerX:int;
		private var centerY:int;
		private var Pointer_H:Shape;
		private var Pointer_M:Shape;
		private var Pointer_S:Shape;
        private var currentTime:Date;
		private var NumLdr:Loader;
		private var NumURLReq:URLRequest;   //load number.png

		public function clockskin(W:uint,H:uint)
		{
			this.W = W;
			this.H = H;
			
			this.radius_W = Math.round(this.W / 2);
			this.radius_H = Math.round(this.H / 2);
			
			this.centerX = this.radius_W;
			this.centerY = this.radius_H;
		}
		//set base
		public function init():void
		{
			//draw background			
			draw_Circle(0xAAAAAA,0x333333,275,200,150);
			draw_Circle(0xAAAAAA,0x333333,275,200,145);
			
			//load number
			NumLdr = new Loader();
			NumURLReq = new URLRequest("number.png");
			NumLdr.load(NumURLReq);
			NumLdr.x=139;
			NumLdr.y=59;
			NumLdr.scaleX=1.01;
			NumLdr.scaleY=1.01;
			this.addChild(NumLdr);
		
			//draw pointer
			creat_Pointer();

		}
		private function creat_Pointer():void
		{
			var Pointer_S_Shape:Shape = new Shape();
			draw_Rect(Pointer_S_Shape,4,-130,0xffffff);
        	this.Pointer_S = Shape(addChild(Pointer_S_Shape));
        	this.Pointer_S.x = centerX;
        	this.Pointer_S.y = centerY;
			//this.Pointer_S.rotation=60;   //test rotation 
			
			var Pointer_M_Shape:Shape = new Shape();
			draw_Rect(Pointer_M_Shape,6,-110,0xffDDDD);
        	this.Pointer_M = Shape(addChild(Pointer_M_Shape));
        	this.Pointer_M.x = centerX;
        	this.Pointer_M.y = centerY;
			//this.Pointer_M.rotation=60;   //test rotation
			
			var Pointer_H_Shape:Shape = new Shape();
			draw_Rect(Pointer_H_Shape,-8,-80,0xffBBBB);
        	this.Pointer_H = Shape(addChild(Pointer_H_Shape));
        	this.Pointer_H.x = centerX;
        	this.Pointer_H.y = centerY;
			//this.Pointer_H.rotation=30;   //test rotation
			
			
		}
		//type_w:pointer's width  type_l:pointers's length
		private function draw_Rect(Pointer:Shape,type_w:Number,type_l:Number, color:uint):void
		{
			Pointer.graphics.beginFill(color);
        	Pointer.graphics.drawRect(0,2,type_w,type_l);
			Pointer.graphics.endFill();
		}
		//draw base cicle
		private function draw_Circle (color_l:uint,color_b:uint,s_x:int,s_y:int,s_r:int):void
		{
			this.graphics.lineStyle(2,color_l);
			this.graphics.beginFill(color_b);
			this.graphics.drawCircle(s_x,s_y,s_r);
			this.graphics.endFill();
		}
		
		//Time
        public function getTime():void
        {
        	//current date and time in an instance variable
        	currentTime = new Date();
        	showTime(currentTime);
        }
		
        private function showTime(time:Date):void 
        {
        	// Gets the time values
	        var seconds:uint = time.getSeconds();
	        var minutes:uint = time.getMinutes();
	        var hours:uint = time.getHours();
	        
	        //360 / 60 = 6 degrees
	        this.Pointer_S.rotation = seconds * 6;
	        this.Pointer_M.rotation = minutes * 6;
	        
	        // Multiplies by 30 to get basic degrees, and then
	        // adds up to 29.5 degrees (59 * 0.5) to account 
	        // for the minutes
	        this.Pointer_H.rotation = (hours * 30) + (minutes * 0.5);
		}
		
	}
}